package com.zzh.demo.core.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import lombok.Data;

import java.util.List;

@JsonInclude(value=JsonInclude.Include.NON_NULL)
@Data
public class ResponseModel<E, D> {

	private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";
	private static final String DEFAULT_ERROR_MESSAGE = "操作失败";
	private static final Integer CODE_SUCCESS = 1000;
    private static final Integer CODE_FAILURE_DEFAULT = 999;

	private Integer code = CODE_SUCCESS;
	private boolean success;
	private String message;
	private Integer total;// 总条数
	private Integer pageNum;// 当前页码
	private Integer pageSize;// 每页条数
	private D data;
	private List<E> rows;


    public ResponseModel() {
    }


    private ResponseModel(Integer code, boolean success, String message, D data, Integer total, Integer pageNum,
                          Integer pageSize) {
	    this.code = code;
		this.success = success;
		this.message = message;
		this.data = data;
		this.total = total;
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

    private ResponseModel(boolean success, String message, D data, Integer total, Integer pageNum, Integer pageSize) {
        this(success ? CODE_SUCCESS: CODE_FAILURE_DEFAULT, success, message, data, total, pageNum, pageSize);
    }


    public ResponseModel(Page page) {
	    this(Boolean.TRUE, DEFAULT_SUCCESS_MESSAGE, null, (int) page.getTotal(), page.getPageNum(), page.getPageSize());
        this.rows = page.getResult();
    }

    public ResponseModel(List page) {
        this(Boolean.TRUE, DEFAULT_SUCCESS_MESSAGE, null, (Integer) page.size(), 1, 0);
        this.rows = page;
    }


    public static ResponseModel success() {
		return success(DEFAULT_SUCCESS_MESSAGE, null, null, null);
	}

	public static ResponseModel success(String message, Integer total, Integer pageNum, Integer pageSize) {
		return new ResponseModel(1000, Boolean.TRUE, message, null, total, pageNum, pageSize);
	}

	public static <D> ResponseModel success(D data, Integer total, Integer pageNum, Integer pageSize) {
		return success(null, data, total, pageNum, pageSize);
	}

	public static <D> ResponseModel success(D data) {
		return success(null, data, null, null, null);
	}

	public static <D> ResponseModel success(String message, D data, Integer total, Integer pageNum, Integer pageSize) {
		return new ResponseModel(CODE_SUCCESS, Boolean.TRUE, message, data, total, pageNum, pageSize);
	}

	public static ResponseModel failure() {
		return failure(DEFAULT_ERROR_MESSAGE);
	}

    public static ResponseModel failure(String message) {
        return new ResponseModel(Boolean.FALSE, message, null, null, null, null);
    }

	public static ResponseModel failure(Integer code, String message) {
		return new ResponseModel(code, Boolean.FALSE, message, null, null, null, null);
	}

	public static <D> ResponseModel failure(D data) {
		return failure((String)null, data);
	}

    public static <D> ResponseModel failure(Integer code, D data) {
        return failure(code, null, data);
    }

	public static <D> ResponseModel failure(String message, D data) {
		return new ResponseModel(Boolean.FALSE, message, data, null, null, null);
	}

    public static <D> ResponseModel failure(Integer code, String message, D data) {
        return new ResponseModel(code, Boolean.FALSE, message, data, null, null, null);
    }

	public boolean isSuccess() {
		return success;
	}

	public String getMessage() {
		return message;
	}

	public D getData() {
		return data;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}


    public List getRows() {
        return rows;
    }


    public ResponseModel setRows(List rows) {
        this.rows = rows;
        return this;
    }


    public Integer getCode() {
        return code;
    }


    @Override
	public String toString() {
		return "Appr [success=" + success + ", message=" + message + ", data=" + data + ", total=" + total
				+ ", pageNum=" + pageNum + ", pageSize=" + pageSize + "]";
	}

}
